<?php
namespace app\task;

use Db;
use Http;
use Rs;

class Worker
{

    public function __call($m, $o)
    {
        return $this;
    }

    public static function run()
    {
        $h = new self;
        $h->initTasker();
        \Swoole\Timer::tick(1000, fn() => $h->getTasker());
        \Swoole\Event::wait();

        //获取参数 POST ucode  fingerprint  http://5s4jhfzz2n6t.xzmzjbq.museum.mw:8200/client/getClientConfigurationParameter

        //获取参数  http://5s4jhfzz2n6t.xzmzjbq.museum.mw:8200/client/getChatRecords

        //提交日志 http://5s4jhfzz2n6t.xzmzjbq.museum.mw:8200/client/uploadLog

        // ws://5s4jhfzz2n6t.xzmzjbq.museum.mw:9000/clientWs?ucode=aj82c9JU&fingerprint=xhg7ihNhmZ
    }

    public function getTasker()
    {
        $wait = Db::table('tasker')->where('status', 2)->where('nextTime', '<', time())->select();
        foreach ($wait as $k => $v) {
            go(fn() => $this->runTasker($v));
        }
        return $this;
    }

    //生成需要重复访问的序列ID
    public function initTasker()
    {
        Db::table('tasker')->where('id', '>', 1)->update(['status' => 1, 'currNumber' => 0, 'lastTime' => 0, 'nextTime' => 0]);
        $wait = Db::table('tasker')->where('status', 1)->fields('id,mostNumber,total')->select();

        foreach ($wait as $k => $v) {
            $randomNumbers = [];
            for ($i = 0; $i < $v->mostNumber; $i++) {
                $randomNumber = rand(1, $v->total);
                // 检查随机数是否已存在于数组中
                while (in_array($randomNumber, $randomNumbers)) {
                    $randomNumber = rand(1, $v->total);
                }
                $randomNumbers[] = $randomNumber;
            }
            \Rs::del('ym:tasker:' . $v->id);
            \Rs::sadd('ym:tasker:' . $v->id, ...$randomNumbers);
            Db::table('tasker')->where('status', 1)->update(['status' => 2]);
            loger("任务$v->id 生成重复访问随机ID成功 " . implode(',', $randomNumbers));
        }

    }

    public function runTasker($v)
    {
        if ($v->currNumber == 0) {
            loger("任务ID $v->id 开始");
        }
        //判断当前进度ID是否需要重复访问
        $currNumber = $v->currNumber + 1;
        if (Rs::Sismember('ym:tasker:' . $v->id, $currNumber)) {
            $cf  = true;
            $cfn = '本进度ID需要重复访问';
        } else {
            $cf  = false;
            $cfn = '';
        }

        $interval = rand($v->intervalTimeMin, $v->intervalTimeMax);
        $stop     = rand($v->stopTimeMin, $v->stopTimeMax);
        loger("任务ID $v->id : 进度 $currNumber/$v->total 停留$v->stopTimeMin/$v->stopTimeMax=$stop 秒 访问间隔$v->intervalTimeMin/$v->intervalTimeMax=$interval 秒 $cfn");
        $time     = time();
        $nextTime = $time + $interval;

        Db::raw("UPDATE tasker SET currNumber = $currNumber,lastTime = $time ,nextTime=$nextTime WHERE id = $v->id")->execute();
        if ($currNumber == $v->total) {
            loger("任务ID $v->id 结束");
            Db::table('tasker')->where('id', $v->id)->update(['status' => 3]);
        }
        sleep($stop);

    }

    public function getOpt()
    {
        // loger('获取任务参数');
        // sleep(1);

    }

    public function getProxy($exp = 5)
    {
        $url = 'http://bapi.51daili.com/getapi2?linePoolIndex=-1&packid=2&time=' . $exp . '&qty=1&port=1&format=json&field=ipport,expiretime,regioncode,isptype&dt=4&ct=1&usertype=17&uid=41903&accessName=liepan&accessPassword=32D632467739711541651C13D74B90C0&skey=autoaddwhiteip';

        $a = Http::url($url)->method('GET')->toJson();
        loger($a);
    }

    public function getCodeUrl($code = 'qzYuz4RBiw5VFpxDfnsjmDt')
    {
        $url    = 'http://xzyfcx333.mc80672.shop:8200/api/getQrUrl02?codes=' . $code;
        $result = Http::url($url)->method('GET')->toJson();
        loger($result);
    }

}
